package com._520it.crm.service.impl;

import com._520it.crm.domain.Employee;
import com._520it.crm.domain.UnderWriting;
import com._520it.crm.mapper.UnderWritingMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.UnderWritingQueryObject;
import com._520it.crm.service.IUnderWritingService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class UnderWritingServiceImpl implements IUnderWritingService {

    @Autowired
    private UnderWritingMapper underWritingMapper;

    @Override
    public int insert(UnderWriting record) {
        return underWritingMapper.insert(record);
    }

    @Override
    public UnderWriting selectByOrderBillId(Long id) {
        return underWritingMapper.selectByOrderBillId(id);
    }

    @Override
    public List<UnderWriting> selectAll() {
        return underWritingMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(UnderWriting record) {
        return underWritingMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryPage(UnderWritingQueryObject qo) {
        Long count = underWritingMapper.queryPageCount(qo);
        if(count <= 0){
            return new PageResult(0L, Collections.EMPTY_LIST);
        }
        List<Map> result =  underWritingMapper.queryPageData(qo);
        return new PageResult(count, result);
    }

    @Override
    public int updatePassStatus(Long underWritingId, String param) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        UnderWriting underWriting = new UnderWriting();
        underWriting.setId(underWritingId);
        underWriting.setComments(param);
        underWriting.setAuditor(employee);
        underWriting.setStatus(UnderWriting.AUDITED);
        return underWritingMapper.updatePassStatus(underWriting);
    }

    @Override
    public int updateNotPassStatus(Long underWritingId, String param) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        UnderWriting underWriting = new UnderWriting();
        underWriting.setId(underWritingId);
        underWriting.setSuggestions(param);
        underWriting.setAuditor(employee);
        underWriting.setStatus(UnderWriting.RETURN_TO_BE_MODIFIED);
        return underWritingMapper.updateNotPassStatus(underWriting);
    }

    @Override
    public Map<String,String> querySuggestions(Long orderBillId) {
        return underWritingMapper.querySuggestions(orderBillId);
    }

    @Override
    public PageResult queryApprovedPage(UnderWritingQueryObject qo) {
        Long count = underWritingMapper.queryApprovedCount(qo);
        if(count <= 0){
            return new PageResult(0L, Collections.EMPTY_LIST);
        }
        List<Map> result =  underWritingMapper.queryApprovedData(qo);
        return new PageResult(count, result);
    }
}
